package com.example.xuegengwang.xuegengwang.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Toast;

import com.example.xuegengwang.xuegengwang.R;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;

import java.util.HashMap;

import cn.sharesdk.wechat.utils.WXAppExtendObject;
import cn.sharesdk.wechat.utils.WXMediaMessage;
import cn.sharesdk.wechat.utils.WechatHandlerActivity;
import common.Constant;
import https.InterNetController;
import utils.MyLogUtils;
import utils.SPUtils;
import utils.ToastUtils;
import view.WaitingDialog;

/**
 * Created by Administrator on 2017/4/19.
 * 微信登录页
 */
public class WXEntryActivity extends WechatHandlerActivity implements IWXAPIEventHandler {
    private static final String TAG = "WXEntryActivity";
    private String ac_id;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    try {
                        JSONObject jsonObject = new JSONObject(String.valueOf(msg.obj));
                        String err_code = jsonObject.getString("err_code");
                        if("0".equals(err_code)){
                            //绑定成功
                            Intent i = new Intent();
                            i.setAction(Constant.WEIXIN_ACITION_HASBIND);
                            sendBroadcast(i);
                            WXEntryActivity.this.finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;
                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ac_id = SPUtils.getSharedPreferences().getString(Constant.USER_ID, "");
        MyWxApi.iwxapi.handleIntent(getIntent(), this);
    }

    public void onGetMessageFromWXReq(WXMediaMessage msg) {
        if (msg != null) {
            Intent iLaunchMyself = getPackageManager().getLaunchIntentForPackage(getPackageName());
            startActivity(iLaunchMyself);
        }
    }

    /**
     * 处理微信向第三方应用发起的消息
     * <p>
     * 此处用来接收从微信发送过来的消息，比方说本demo在wechatpage里面分享
     * 应用时可以不分享应用文件，而分享一段应用的自定义信息。接受方的微信
     * 客户端会通过这个方法，将这个信息发送回接收方手机上的本demo中，当作
     * 回调。
     * <p>
     * 本Demo只是将信息展示出来，但你可做点其他的事情，而不仅仅只是Toast
     */
    public void onShowMessageFromWXReq(WXMediaMessage msg) {
        if (msg != null && msg.mediaObject != null
                && (msg.mediaObject instanceof WXAppExtendObject)) {
            WXAppExtendObject obj = (WXAppExtendObject) msg.mediaObject;
            Toast.makeText(this, obj.extInfo, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onReq(BaseReq baseReq) {
        MyLogUtils.e(TAG, "" + baseReq.openId);
    }

    @Override
    public void onResp(BaseResp baseResp) {
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                String code = ((SendAuth.Resp) baseResp).code;
                //确认登录
                if (!TextUtils.isEmpty(ac_id)) {
                    //绑定微信
                    bindWX(code);
                } else {
                    //登录微信
                    Intent i = new Intent();
                    i.putExtra("code", code);
                    i.setAction(Constant.WEIXIN_ACITION_HASLOAD);
                    sendBroadcast(i);
                    WXEntryActivity.this.finish();
                }
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                //用户取消
                WXEntryActivity.this.finish();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                //登录失败
                ToastUtils.shortToast("登录失败");
                WXEntryActivity.this.finish();
                break;
            default:
                WXEntryActivity.this.finish();
                break;
        }
    }

    private void bindWX(String code) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("code", code);
        hashMap.put("type","wx");
        new InterNetController(this, Constant.BIND, handler, hashMap, 0);
    }
}
